var pageNo = 1
var pageSize = 3
$(document).ready(function(){
    if($.cookie('Authorization')===null || localStorage.getItem('realname') === null) {
        showErr("您处于未登录状态，请先登录", '1000000');
        return
    }
    $('#realname').text(localStorage.getItem('realname') )

    $("#wallCarousel").carousel('cycle');
   
    $("#hzCarousel").carousel('cycle');
    $('#hzCarousel').on('slide.bs.carousel', function (event) {
        var $hoder = $('#hzCarousel').find('.item')
        var $items = $(event.relatedTarget)
        //getIndex就是轮播到当前位置的索引
        var getIndex= $hoder.index($items)
        $('.num-box>span').text(getIndex+1)
    });

    $('.search-btn').click(function(){
        if ($('#partner-search').val() === '') {
            localStorage.setItem('searchParam', JSON.stringify({
                pageNo: 1,
                pageSize:10
            }))
        } else {
            localStorage.setItem('searchParam', JSON.stringify({
                pageNo: 1,
                pageSize:10,
                entName: $('#partner-search').val()
            }))
        }
        window.location.href = host_url + "search.html"
    })

    $('.bar-main-right').click(function(){
        localStorage.setItem('searchParam', JSON.stringify({
            pageNo: 1,
            pageSize:10
        }))
        window.location.href = host_url + "theme.html";
    })
    $('.bar-main-left').click(function(){
        window.location.href = host_url + "partner.html";
    })

    $('.partner-box2-item span, .partner-box2-item h2').click(function(){
        pageNo = 1
        pageSize = 3
        $('.partner-box2-item span').removeClass('active')
        var zkFl1 = ''
        var zkFl2 = ''
        if ($(this)[0].tagName == 'H2') {
            var zkFl1 = $(this).text()
            var zkFl2 = $(this).text()
        } else {
            $(this).addClass('active')
            var zkFl1 = $(this).siblings('h2').text()
            var zkFl2 = $(this).text()
        }
        $('.business-box').hide()
        loadData(zkFl1,zkFl2)
        
    })
})

// 加载数据
function loadData(zkFl1,zkFl2) {
    var paramStr = ''
    if (zkFl1 === zkFl2) {
        paramStr = 'isCooperate=1&zkFl1='+encodeURIComponent(zkFl1)+'&pageNo='+pageNo+'&pageSize='+pageSize+'&token=' + $.cookie('Authorization')
    } else {
        paramStr = 'isCooperate=1&zkFl1='+encodeURIComponent(zkFl1)+'&zkFl2='+encodeURIComponent(zkFl2)+'&pageNo='+pageNo+'&pageSize='+pageSize+'&token=' + $.cookie('Authorization')
    }
    $('.business-main').empty()
    $.ajax({
        url: service_url + '/a/zhiku/zkEntInfo/advancedSerach?'+paramStr,
        type: 'GET',
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true,
        contentType: 'application/json',
        dataType: 'jsonp',
        jsonpCallback:"success_jsonpCallback",  
        processData: false, // 告诉jQuery不要去处理发送的数据
        success: function(data) {
            if (data.status === "1000000" || data.status ==="01") {
                showErr("您处于未登录状态，请先登录", '1000000')
                return
            }
            
            for (var i = 0; i < data.data.length; i++) {
                var item = data.data[i]
                var innerHtml = ''
                var linkHtml = ''
                var name = ''
                var position = ''
                var tel = ''
                if (item.contacts.length > 0) {
                    name = item.contacts[0].name || ''
                    position = item.contacts[0].position || ''
                    tel = item.contacts[0].tel || ''
                }
                var regcap = item.regcap / 10000 >= 1 ? (item.regcap / 10000) + '亿元' : item.regcap + '万元'
                var tagHtml = '<span>'+regcap+'</span>'
                if (item.field !== undefined) {
                    var fieldArr = item.field.split(',')
                    for (var i = 0; i < fieldArr.length; i++) {
                        if (fieldArr[i] !== '') {
                            tagHtml = tagHtml + '<span>'+fieldArr[i]+'</span>'
                        }
                    }
                }
                var tempNum = 0
                for (var n = 0; n < item.linkList.length; n++) {
                    if (item.linkList[n].description !== undefined && item.linkList[n].description !== '' && tempNum < 2) {
                        tempNum++
                        var description = item.linkList[n].description
                        if (item.linkList[n].description.length > 50) {
                            description = item.linkList[n].description.slice(0,50) + '...'
                        }
                        if (item.linkList[n].zkPictureMessageList !== undefined && item.linkList[n].zkPictureMessageList.length > 0 && item.linkList[n].zkPictureMessageList[0].id !== 0) {
                            linkHtml = linkHtml + '<li><img src="'+service_url + '/a/zhiku/zkLinkInteraction/getPic?id='+item.linkList[n].zkPictureMessageList[0].id+'"><p>'+description+'</p></li>'
                        } else {
                            linkHtml = linkHtml + '<li><p>'+description+'</p></li>'
                        }
                    }
                }
                innerHtml = '<div class="item"><div class="business-item"><div class="business-item-left"><h2>'+item.entName+'</h2><div class="business-info-box"><span>'+name+'</span><span>'+position+'</span><span>'+tel+'</span></div><div class="business-tag-box">'+tagHtml+'</div></div><div class="business-item-right"><ul>'+linkHtml+'</ul></div></div>'
                $('.business-main').append(innerHtml)
                $('.pagination').jqPaginator({
                    totalCounts: data.count,
                    pageSize: pageSize,
                    visiblePages: 10,
                    currentPage: pageNo,
                    first: '<li class="first"><a href="javascript:void(0);">首页<\/a><\/li>',
                    prev: '<li class="prev"><a href="javascript:void(0);">&laquo;<\/a><\/li>',
                    next: '<li class="next"><a href="javascript:void(0);">&raquo;<\/a><\/li>',
                    last: '<li class="last"><a href="javascript:void(0);">末页<\/a><\/li>',
                    page: '<li class="page"><a href="javascript:void(0);">{{page}}<\/a><\/li>',
                    onPageChange: function (num, type) {
                        if (type === 'change') {
                            pageNo = num
                            loadData(zkFl1,zkFl2)
                        }
                    }
                });
                $('.business-box').show()
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            showErr("您处于未登录状态，请先登录", '1000000')
        }
    })
}